<script setup lang="ts">
import { ref, reactive, computed } from "vue";
import { useColumns } from "./columns";
import { tableDataTemp } from "./data";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import "plus-pro-components/es/components/search/style/css";

import { type PlusColumn, PlusSearch } from "plus-pro-components";
import { PureTableBar } from "@/components/RePureTableBar";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import AddFill from "@iconify-icons/ri/add-circle-line";

const query = ref({
  dictCode: "",
  dictName: "",
  dictLocalName: "",
  dictEnglishName: "",
  dictType: ""
});

const form = ref({
  dictCode: "",
  dictName: "",
  dictLocalName: "",
  dictEnglishName: "",
  dictType: "",
  sortNumber: 0,
  parentId: 0,
  isUsed: 1,
  remark: ""
});

const visible = ref(false);
const modalTitle = ref("新增");
const currentRow = ref(null);
const fileList = ref([]);
const formDisabled = computed(() => modalTitle.value === "查看");

const dictTypeOptions = [
  { label: "证书类型", value: "1" },
  { label: "字典类型2", value: "2" },
  { label: "字典类型3", value: "3" }
];

const searchColumns: PlusColumn[] = [
  {
    label: "字典编号",
    prop: "dictCode"
  },
  {
    label: "字典简体名称",
    prop: "dictName"
  },
  {
    label: "字典繁体名称",
    prop: "dictLocalName"
  },
  {
    label: "字典英文名称",
    prop: "dictEnglishName"
  },
  {
    label: "字典类型",
    prop: "dictType",
    type: "select",
    options: dictTypeOptions
  }
];

const handleChange = (values: any) => {
  console.log(values, "change");
};
const handleSearch = (values: any) => {
  console.log(values, "search");
};
const handleRest = () => {
  console.log("handleRest");
};
const openDialog = (title: any, row?: any) => {
  if (row) {
    currentRow.value = row;
    form.value = { ...row };
  }
  modalTitle.value = title;
  visible.value = true;
};
const closeDialog = () => {
  currentRow.value = null;
  form.value = {
    dictCode: "",
    remark: "",
    dictName: "",
    dictLocalName: "",
    dictEnglishName: "",
    dictType: "",
    sortNumber: 0,
    parentId: 0,
    isUsed: 1
  };
  fileList.value = [];
  visible.value = false;
};
const resetDialogForm = () => {
  if (modalTitle.value === "新增") {
    form.value = {
      dictCode: "",
      remark: "",
      dictName: "",
      dictLocalName: "",
      dictEnglishName: "",
      dictType: "",
      sortNumber: 0,
      parentId: 0,
      isUsed: 1
    };
  } else {
    form.value = { ...currentRow.value };
  }
};
const changeFormTitle = () => {
  modalTitle.value = "修改";
};
const handleUploadSuccess = (res: any, file: any) => {
  console.log(res, file, "handleUploadSuccess");
};
const handleUploadError = (err: any, file: any) => {
  console.log(err, file, "handleUploadError");
};
const handleUploadRemove = (file: any) => {
  console.log(file, "handleUploadRemove");
};
const handleDelete = row => {
  console.log(
    "%crow===>>>: ",
    "color: MidnightBlue; background: Aquamarine; font-size: 20px;",
    row
  );
};

const tableRef = ref();

const {
  loading,
  columns,
  pagination,
  loadingConfig,
  onSizeChange,
  onCurrentChange
} = useColumns();
</script>

<template>
  <div>
    <PlusSearch
      v-model="query"
      :columns="searchColumns"
      :show-number="2"
      label-width="120"
      label-position="right"
      @change="handleChange"
      @search="handleSearch"
      @reset="handleRest"
    />
    <PureTableBar title="字典列表" :columns="columns" @refresh="handleSearch">
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog('新增')"
        >
          新增字典
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          row-key="id"
          alignWhole="center"
          table-layout="auto"
          border
          sortable
          showOverflowTooltip
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          :size="size"
          :loading="loading"
          :loading-config="loadingConfig"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          :data="
            tableDataTemp.slice(
              (pagination.currentPage - 1) * pagination.pageSize,
              pagination.currentPage * pagination.pageSize
            )
          "
          @page-size-change="onSizeChange"
          @page-current-change="onCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon('ri:search-line')"
              @click="openDialog('查看', row)"
            >
              查看
            </el-button>
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('修改', row)"
            >
              修改
            </el-button>
            <el-popconfirm
              width="200px"
              :title="`是否确认删除字典：${row.dictCode}？`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
    <el-drawer
      v-model="visible"
      :title="modalTitle"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      direction="rtl"
      size="50%"
    >
      <el-form :model="form" label-width="auto" :disabled="formDisabled">
        <el-form-item prop="dictCode" label="字典编号" required>
          <el-input
            v-model="form.dictCode"
            autocomplete="off"
            :disabled="formDisabled"
          />
        </el-form-item>
        <el-form-item prop="dictName" label="字典名称" required>
          <el-input
            v-model="form.dictName"
            autocomplete="off"
            placeholder="请输入字典名称"
          />
        </el-form-item>
        <el-form-item prop="dictName" label="字典繁体名称">
          <el-input
            v-model="form.dictName"
            autocomplete="off"
            placeholder="请输入字典繁体名称"
          />
        </el-form-item>
        <el-form-item prop="dictName" label="字典英文名称">
          <el-input
            v-model="form.dictName"
            autocomplete="off"
            placeholder="请输入字典英文名称"
          />
        </el-form-item>
        <el-form-item prop="dictType" label="字典类型" required>
          <el-select
            v-model="form.dictType"
            placeholder="请选择字典类型"
            :disabled="formDisabled"
          >
            <el-option
              v-for="item in dictTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="sortNumber" label="排序号">
          <el-input-number
            v-model="form.sortNumber"
            autocomplete="off"
            :disabled="formDisabled"
          />
        </el-form-item>
        <el-form-item prop="parentId" label="父级字典">
          <el-input
            v-model="form.parentId"
            autocomplete="off"
            :disabled="formDisabled"
          />
        </el-form-item>
        <el-form-item prop="remark" label="备注">
          <el-input
            v-model="form.remark"
            autocomplete="off"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button
            v-show="!formDisabled"
            type="warning"
            @click="resetDialogForm"
            >重 置</el-button
          >
          <el-button
            v-show="formDisabled"
            type="warning"
            @click="changeFormTitle"
            >修 改</el-button
          >
          <el-button type="primary">确 定</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>
